//
//  SLNavigationController.swift
//  SLBlog
//
//  Created by 盼 on 2017/11/23.
//  Copyright © 2017年 pan. All rights reserved.
//

import UIKit

class SLNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        interactivePopGestureRecognizer?.delegate = self
    }
    
    @objc func popToParentVC() {
        popViewController(animated: true)
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if childViewControllers.count>0 {
            
           viewController.hidesBottomBarWhenPushed = true
            
            let vc = viewController as! SLBaseViewController
            
            var title = "返回"
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.navigationItem.title ?? "返回"
            }
            
            vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParentVC), isBack: true)
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        return childViewControllers.count != 1;
    }
}




